//
//  WKUserAccount.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/26.
//  Copyright © 2016年 恋guang年. All rights reserved.
/* "access_token" = "2.00jRmV6G0e6H2l469e0e33a5CznyeB";
"expires_in" = 157679999;
"remind_in" = 157679999;
uid = 5873779171;*/

import UIKit

class WKUserAccount: NSObject,NSCoding{
    // 接口获取授权后的access token
    var access_token: String?
    // access_token的生命周期，单位是秒数 基本数据类型要初始化 不然转模型找不到
    //    var expires_in:NSTimeInterval = 0
    // 当前授权用户的UID
    var uid: String?
    //  过期时间
    var expiresDate: NSDate?
    //用户名
    var screen_name:String?
    //用户头像地址 180×180像素
    var avatar_large:String?
    
    var expires_in :NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    
    init(dict: [String :AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // 对象打印信息
    override var description: String {
        let keys = ["access_token", "uid", "expires_in","expiresDate"]
        return dictionaryWithValuesForKeys(keys).description
    }
    //不重写此方法 一键转模型 找不到对应的key会崩
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("UndefinedKey=\(key)")
    }
    // MARK: - 归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(expires_in,forKey: "expires_in")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        expires_in = (aDecoder.decodeObjectForKey("expires_in") as? NSTimeInterval)!
        screen_name = aDecoder.decodeObjectForKey( "screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey( "avatar_large") as? String
        
    }
}

















